<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */
 
$prj = new project;
$prj->status=STATUS_ACTIVE;
$list = $loader->loadListBy($prj,$prj->listByStatus());

if ($CURRENT_USER->groupid == GROUP_ADMIN) {
	$slist = $list;
} else {
	$slist = array();
	foreach ($list as $obj) {
		$file = new files($obj->id);
		$file->teamid=getTeamId($CURRENT_USER);
		$i = $loader->fetchcount($file->countTeam());
		if ($i>0) {
			array_push($slist,$obj);
		}
	}
}

?>
<script language="javascript">
	var readOnly = new Array();
	var FixedProjectItemRecord = Ext.data.Record.create([{name: 'id'},{name: 'name'},{name: 'statsupdated'}, {name: 'upd'}]);
	var FileListProjectDS = new Ext.data.Store({
		autoLoad: false,
		proxy: new Ext.data.HttpProxy({
			url: getMainUrl(),
			method: 'POST'
			}), 
		baseParams: {action: 'system_list', op: 'prjlt'},
		reader: new Ext.data.JsonReader({
			root: 'results',
			id: 'id'
		},FixedProjectItemRecord),
	    listeners: {
	        ' load': {
	            fn: function(store,records,opt){
	            	FileListMenu.removeAll();
				    store.each(function(record) {
							var elem = this;
							var title = record.data.name; 
							if (record.data.statsupdated!="") {
								title += " " + String.format(_('[Statistics updated on {0}]'),record.data.statsupdated);
							}
							FileListMenu.add(
								{text: record.data.name,
								 filePid: record.data.id,
								 handler: elem.createWindow,
								 scope: elem,
								 windowId: 'filelist_' + record.data.id,
								 iconCls: 'plugin',
								 title: title
								}
								);		
						});
	            }
	        }
	    }
	});
	
<?php
	foreach ($slist as $obj) {
		$upd = util::fromUTC($obj->statsupdated,$CURRENT_USER->timezone);
		if ($obj->upd == false) {
			echo "readOnly[$obj->id]=true;";
			echo "FileListProjectDS.add(new FixedProjectItemRecord({id: $obj->id,name: '$obj->name',statsupdated: '$upd', ro: true})); ";
		} else {
			echo "FileListProjectDS.add(new FixedProjectItemRecord({id: $obj->id,name: '$obj->name',statsupdated: '$upd', ro: false})); ";
		}
	}
	
	if (defined('UPDATE_PROJECT_FREQ')) {
		echo "var UPFREQ='".UPDATE_PROJECT_FREQ."';";
?>
	function updateProjectDS() {
		FileListProjectDS.load();
		setTimeout(updateProjectDS,UPFREQ);
/*
		Ext.Ajax.request({ 
		            url: getMainUrl(), 
		            params: { 
		               action: "system_list",
		               op: "prjlt" 
		              }, 
		            success: function(response){
		            	var obj = Ext.util.JSON.decode(response.responseText);
						
						if (obj.success) {
							var list = obj.ret.results;
							for(var i=0;i<list.length;i++) {
								var r = FileListProjectDS.getById(list[i].id);
								if (r!=undefined) {
									r.beginEdit();
									r.set('statsupdated',list[i].statsupdated);
									r.commit();
								}
							}
		            	}
		            	
		            	setTimeout(updateProjectDS,UPFREQ);
		            }
	   });
*/	   								
	}
	
	setTimeout(updateProjectDS,UPFREQ);
<?php } ?>	

</script>